﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataLib.Accounts.Bus;


namespace Ewts.Storage
{
    public partial class VehicleArrangeList2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                if (Session["UserInfo"] != null)
                {
                    User curUser = new User();
                    curUser = (User)Session["UserInfo"];
                    ListItem item = new ListItem();
                    item.Text = "--请选择--";
                    item.Value = "";
                    DataLib.BLL.City bllLib = new DataLib.BLL.City();
                    // 处理地
                    dropGraphic.DataSource = bllLib.GetList(" flag1 ='02'");
                    dropGraphic.DataTextField = "cityname";
                    dropGraphic.DataValueField = "cityname";
                    dropGraphic.DataBind();
                    dropGraphic.Items.Insert(0, item);


                    string city = "";
                    if (curUser.UserType != "AD")
                    {
                        dropGraphic.SelectedValue = curUser.CompanyCity;
                        city = dropGraphic.SelectedValue;
                        dropGraphic.Enabled = false;
                    }


                    //bindData(city);
                    

                    //FIFA 2015年5月2日 15:17:55 取消派车
                    string act = Request["act"];
                    int vaid = 0;
                    int.TryParse(Request["vaid"],out vaid);
                    if (act == "cancle" && vaid>0)
                    {
                        DelVh(vaid);
                    }


                }
                else
                {
                    Response.Redirect("/Accounts/Login.aspx");                                 
                }
            }
        }

        //绑定数据
        public void bindData(string city)
        {
            DataTable dt = new DataLib.BLL.InStorage().GetInMangeList(city);

           
                //rept.DataSource = dt;
                //rept.DataBind();
          
        }


        //获取单据重打跳转的地址
        protected string getUrlWhem(string instorageno)
        {
            string result = "";
            string str = instorageno.Substring(0, 1);
            int id = 0;
            DataTable dt = new DataLib.BLL.InStorage().GetList(" wrlno= '" + instorageno+"'").Tables[0];
            if (dt.Rows.Count!=0||dt!=null)
	        {
                id = Convert.ToInt32(dt.Rows[0][0]);
                if (str == "M")
                {
                    result = "/RDLC/WRLInfoRDLC.aspx?wrlid=" + id;
                }
                else
                    result = "/RDLC/SegregationRDLC.aspx?instorageID=" + id; 
            }
           
            return result;
        }

        /// <summary>
        /// 获取数据维护跳转的地址
        /// </summary>
        /// <param name="instorageno">入库编号</param>
        /// <param name="cjoid"></param>
        /// <param name="outid"></param>
        /// <returns></returns>
        protected string getUrldataMng(string insid)
        { 
            string result = "";
            DataLib.Model.InStorage insmodel = new DataLib.BLL.InStorage().GetModel(int.Parse(insid));
            if (insmodel.outid != 0)
            {
                DataLib.Model.OutStorage oModel = new DataLib.BLL.OutStorage().GetModel((int)insmodel.outid);
                if ((oModel.outStorageType == "5" && oModel.StorageType == "非原料库" && oModel.inDepart == oModel.inDepart && oModel.OutDepart == oModel.graphicCity)
                        || oModel.outStorageType == "1" || oModel.outStorageType == "2" ||  oModel.outStorageType == "4")
                {
                    result = "/storage/InStorage.aspx?insid=" + insmodel.id.ToString();
                }
                else if(oModel.outStorageType == "3")
                {
                    result = "/storage/ProduceStorage.aspx?insid=" + insmodel.id.ToString();
                }
                else
                {
                    result = "/storage/InWrl.aspx?outid=" + insmodel.outid.ToString();
                }
            }
            else if (insmodel.wrlno.Substring(0, 2) == "RZ")
            {
                result = "/storage/InStorage.aspx?cjoid=" + insmodel.cjoid.ToString();
            }
            else
            {
                result = "/storage/InWrl.aspx?insid=" + insmodel.id.ToString();
            }
                   
            

            return result;
        }

        protected void dropGraphic_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindData(dropGraphic.SelectedValue);
        }

        private void DelVh(int vaid)
        {
            if (vaid != 0)
            {
                DataLib.Model.VehicleArrange model = new DataLib.Model.VehicleArrange();
                DataLib.BLL.VehicleArrange bll = new DataLib.BLL.VehicleArrange();

                model = bll.GetModel(vaid);
                if (model != null)
                {
                    if (model.outid == 0)
                    {
                        DataLib.Model.Cjo cmodel = new DataLib.Model.Cjo();
                        DataLib.BLL.Cjo cbll = new DataLib.BLL.Cjo();
                        cmodel = cbll.GetModel((int)model.cjoid);
                        cmodel.status = 1;
                        cbll.Update(cmodel);
                    }
                    else
                    {
                        DataLib.Model.OutStorage omodel = new DataLib.Model.OutStorage();
                        DataLib.BLL.OutStorage obll = new DataLib.BLL.OutStorage();
                        omodel = obll.GetModel((int)model.outid);
                        omodel.status = 2;
                        obll.Update(omodel);
                    }
                }
                bll.Delete(model.id);
            }
        }

    }
}